package com.fsserver.web.controller;

import com.fsserver.common.api.ApiResponse;
import com.fsserver.core.entity.PaymentOrder;
import com.fsserver.core.entity.PaymentTransaction;
import com.fsserver.core.service.PaymentOrderService;
import com.fsserver.core.service.PaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 支付控制器
 * Payment Controller
 *
 * @author fsserver
 */
@Slf4j
@RestController
@RequestMapping("/payment")
@Api(tags = "支付管理接口", description = "提供支付订单创建、支付、查询、关闭等功能")
public class PaymentController {

    @Autowired
    private PaymentOrderService paymentOrderService;

    @Autowired
    private PaymentService paymentService;

    /**
     * 获取支付订单
     * Get payment order
     *
     * @param orderNo 订单号
     * @return API响应
     */
    @GetMapping("/order/{orderNo}")
    @ApiOperation(value = "获取支付订单", notes = "根据订单号获取支付订单")
    @ApiImplicitParam(name = "orderNo", value = "订单号", required = true, dataType = "String", paramType = "path")
    public ApiResponse<PaymentOrder> getOrder(@PathVariable String orderNo) {
        PaymentOrder paymentOrder = paymentOrderService.getOrderByOrderNo(orderNo);
        return ApiResponse.success(paymentOrder);
    }

    /**
     * 根据业务订单号获取支付订单
     * Get payment order by business order number
     *
     * @param businessOrderNo 业务订单号
     * @return API响应
     */
    @GetMapping("/order/business/{businessOrderNo}")
    @ApiOperation(value = "根据业务订单号获取支付订单", notes = "根据业务订单号获取支付订单")
    @ApiImplicitParam(name = "businessOrderNo", value = "业务订单号", required = true, dataType = "String", paramType = "path")
    public ApiResponse<PaymentOrder> getOrderByBusinessOrderNo(@PathVariable String businessOrderNo) {
        PaymentOrder paymentOrder = paymentOrderService.getOrderByBusinessOrderNo(businessOrderNo);
        return ApiResponse.success(paymentOrder);
    }

    /**
     * 根据状态查询支付订单列表
     * List payment orders by status
     *
     * @param status 支付订单状态
     * @return API响应
     */
    @GetMapping("/orders/status/{status}")
    @ApiOperation(value = "根据状态查询支付订单列表", notes = "根据支付订单状态查询支付订单列表")
    @ApiImplicitParam(name = "status", value = "支付订单状态", required = true, dataType = "String", paramType = "path")
    public ApiResponse<List<PaymentOrder>> listOrdersByStatus(@PathVariable String status) {
        List<PaymentOrder> paymentOrders = paymentOrderService.listOrdersByStatus(status);
        return ApiResponse.success(paymentOrders);
    }

    /**
     * 查询支付
     * Query payment
     *
     * @param orderNo 订单号
     * @param paymentType 支付类型
     * @return API响应
     */
    @GetMapping("/query")
    @ApiOperation(value = "查询支付", notes = "根据订单号和支付类型查询支付")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "orderNo", value = "订单号", required = true, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "paymentType", value = "支付类型", required = true, dataType = "String", paramType = "query")
    })
    public ApiResponse<PaymentTransaction> queryPayment(@RequestParam String orderNo, @RequestParam String paymentType) {
        PaymentTransaction paymentTransaction = paymentService.queryPayment(orderNo, paymentType);
        return ApiResponse.success(paymentTransaction);
    }
} 